package com.four.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;
import java.util.stream.Stream;

/**
 * @author zhao
 * @date 2023/1/21  0021 13:21:36
 */
@Getter
@AllArgsConstructor
public enum TicketStatus {

    AVAILABLE("可用票据", 1),
    USED("已用票据", 2),
    EXPIRED("过期票据", 3);

    //描述
    private String description;
    //编号
    private Integer code;

    //根据code返回数据
    public static TicketStatus of(Integer code) {
        //判断，必须非空
        Objects.requireNonNull(code);

        //返回values()，获取所有的枚举对象
        return Stream.of(values())
                .filter(ticketStatus -> ticketStatus.code.equals(code))// 过滤
                .findFirst()// 获取
                .orElseThrow(() -> new IllegalArgumentException(code + "not exists"));// 抛异常
    }
}
